package com.mcr.graduateprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcr.graduateprogram.bean.Clientele;
import com.mcr.graduateprogram.bean.GameSpecification;
import com.mcr.graduateprogram.bean.Gamekeys;
import com.mcr.graduateprogram.bean.KeyOrder;
import com.mcr.graduateprogram.mapper.ClienteleMapper;
import com.mcr.graduateprogram.mapper.GameSpecificationMapper;
import com.mcr.graduateprogram.mapper.GamekeysMapper;
import com.mcr.graduateprogram.mapper.KeyOrderMapper;
import com.mcr.graduateprogram.service.IKeyOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author graduateprogram
 * @since 2023-02-25
 */
@Service
public class KeyOrderServiceImpl extends ServiceImpl<KeyOrderMapper, KeyOrder> implements IKeyOrderService {
    @Autowired
    private KeyOrderMapper keyOrderMapper;
    @Autowired
    private GameSpecificationMapper gameSpecificationMapper;
    @Autowired
    private GamekeysMapper gamekeysMapper;
    @Autowired
    private ClienteleMapper clienteleMapper;

    @Override
    @Transactional
    public boolean createOrder(KeyOrder keyOrder) {
        //查询购买客户账户信息
        Clientele clientele = clienteleMapper.selectById(keyOrder.getClienteleId());
        if(clientele.getGameMoney()<keyOrder.getRealPay()){
            return false;
        }
        //查询库存
        GameSpecification gameSpecification = gameSpecificationMapper.selectById(keyOrder.getSpecificationId());
        if(gameSpecification.getNumber()<1){
            return false;
        }
        //处理购买客户账户信息
        clientele.setGameMoney(clientele.getGameMoney()-keyOrder.getRealPay());
        clienteleMapper.updateById(clientele);//客户账户金额扣除
        //处理库存
        gameSpecification.setNumber(gameSpecification.getNumber()-1);
        gameSpecificationMapper.updateById(gameSpecification);//游戏规格库存减1
        //生成订单号
        keyOrder.setOrderCode(keyOrder.getClienteleId()+"***"+LocalDateTime.now());
        //拿取激活码并标识已使用
        Gamekeys gamekeys = gamekeysMapper.selectValidGameKey(keyOrder.getSpecificationId());
        gamekeys.setHasSell(true);
        gamekeysMapper.updateById(gamekeys);
        //生成备注
        keyOrder.setNote("您的激活码为："+gamekeys.getGamekey()+",请尽快激活。");
        //生成订单
        keyOrderMapper.insert(keyOrder);
        return true;
    }

    @Override
    public Page selectByPage(Integer page, Integer pageSize, Long clienteleId) {
        Page<KeyOrder> keyOrderPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<KeyOrder> keyOrderLambdaQueryWrapper=new LambdaQueryWrapper<>();
        keyOrderLambdaQueryWrapper.eq(clienteleId!=null,KeyOrder::getClienteleId,clienteleId)
                                  .orderByDesc(KeyOrder::getCreateTime);
        keyOrderMapper.selectPage(keyOrderPage,keyOrderLambdaQueryWrapper);
        return keyOrderPage;
    }
}
